var types;
$().ready(function() {
	validateRule();

	$("#type").change(function (val){
		types = val.target.value;
		if(val.target.value == '4'){
			$("#typesId").hide()
		}else{
			$("#typesId").show()
		}
	})


});
layui.use(['upload'], function () {
	var upload = layui.upload;
	upload.render({
		elem: '#btnUpload',
		url: '/common/sysFile/upload',
		timeout: 36000000,
		// size: 1024 * 100,
		accept: 'file',
		// exts: 'docx|doc|xlsx|pdf|ppt',
		done: function (r) {
			$("#fileRoute").val(r.data);
			$("#test").html(subFileName(r.data));
		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
$(function () {
	$("#resourceType").change(function () {
		$("#enclosure").val("");
		$("#test").html("");
		var type = $(this).val();
		if (type!=0&&type!='') {
			$("#resourse").show();
			$("#btnUpload").show()
		} else {
			$("#resourse").hide();
		}
	});
});
//证书选择
$("#profileId").change(function () {
	$("#kpointsId").empty();
	$("#know").html("");
	$.ajax({
		url: '/jzweb/itemBank/byKnowledges',
		data :{cprofileId:$(this).val()} ,
		success: function (r) {
			if(r.data.length==0){
				$("#kpointsId").append('<option value="">暂无数据</option>');
			}else{
				$("#kpointsId").append('<option value="">请选择</option>');
				for(var p in r.data){
					$("#kpointsId").append('<option value="'+r.data[p].id+'">'+r.data[p].name+'</option>');
				}
			}

		}
	})
});

//知识点选择
$("#kpointsId").change(function () {
	$.ajax({
		url: '/jzweb/itemBank/byGrades',
		data :{knowId:$(this).val()} ,
		success: function (r) {
			var gradeList = r.data.split(",");
			var list ='';
			if(r.data.length==0){
				list+='暂无数据';
			}else{
				for(var p in gradeList){
					list+='<span><input name="grade" type="checkbox" value="'+gradeList[p]+'"/>'+gradeList[p]+'</span>';
				}
			}
			list+='</select>';
			$("#know").html(list);
		}
	})
});

function save() {

	if( types == '4' ){
		$("#typesId").remove();
	}

	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/examinationQuestions/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			kpointsId : {
				required : true
			},
			profileId : {
				required : true
			},
			itemBankType : {
				required : true
			},
			type : {
				required : true
			},
			introduce : {
				required : true
			},
			topicAnalysis : {
				required : true
			},
			score : {
				required : true
			},
			grade : {
				required : true
			}
		},
		messages : {
			kpointsId : {
				required : icon + "请选择知识点"
			},
			profileId : {
				required : icon + "请选择证书概况"
			},
			itemBankType : {
				required : icon + "请选择题库类型"
			},
			type : {
				required : icon + "请选择题目类型"
			},
			introduce : {
				required : icon + "请输入题目内容名"
			},
			topicAnalysis : {
				required : icon + "请输入题目解析"
			},
			score : {
				required : icon + "请输入分值"
			}
		}
	})
}